package com.frxuexi.park2212.sign

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.SearchView.OnQueryTextListener
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.SPUtils
import com.frxuexi.lib_base.base.BaseActivity
import com.frxuexi.lib_base.entity.SignBean
import com.frxuexi.lib_base.entity.SignBeanItem
import com.frxuexi.lib_base.entity.VisitStatusBean
import com.frxuexi.lib_base.net.Const
import com.frxuexi.park2212.R
import com.frxuexi.park2212.databinding.ActivitySignListBinding
import dagger.hilt.android.AndroidEntryPoint
import java.text.SimpleDateFormat
import java.util.Calendar

@Route(path = "/app/SignListActivity")
@AndroidEntryPoint
class SignListActivity : BaseActivity<ActivitySignListBinding, SignViewModel>() {
    val signStatusAdapter by lazy { SignStatusAdapter() }
    val signAdapter by lazy { SignAdapter() }
    lateinit var signs:List<SignBeanItem>//已经查出某月的集合
    var month=""
    lateinit var signsByMonth:SignBean//根据月份查询的原集合

    var clickNum=0
    val calendar = Calendar.getInstance()
    override fun initData() {
        vm.getSigns.observe(this) {
            signsByMonth=it
            it.forEach {
                if (it.morningCheckIn.isNullOrEmpty()) {
                    it.type = 1
                } else {
                    it.type = 0
                }
            }
            signs=it.filter {
                if(it.morningCheckIn.isNullOrEmpty())
                    it.afternoonCheckIn.split("-")[1]==month
                else
                    it.morningCheckIn.split("-")[1]==month
            }
            signAdapter.addData(signs)
            signAdapter.notifyDataSetChanged()
        }
    }

    override fun initView() {
        month=vm.getMonth()
        binding.headSign.apply {
            headerBack.setOnClickListener {
                finish()
            }
            headerTitle.setText("考勤管理")
            headerAdd.visibility=View.GONE
        }
        val list = listOf<VisitStatusBean>(
            VisitStatusBean("全部", true), VisitStatusBean("正常", false),
            VisitStatusBean("迟到", false), VisitStatusBean("早退", false)
        )
        signStatusAdapter.addData(list)
        binding.signRvstatus.apply {
            adapter = signStatusAdapter
            layoutManager =
                LinearLayoutManager(this@SignListActivity, RecyclerView.HORIZONTAL, false)
        }

        signStatusAdapter.setOnItemClickListener { adapter, view, position ->
            signStatusAdapter.data.forEach {
                it.isCheck = false
            }
            signStatusAdapter.data.get(position).isCheck = true
            signStatusAdapter.notifyDataSetChanged()

            signAdapter.data.clear()
            if(position==0){
                signAdapter.addData(signs)
            }else{
                signAdapter.addData(signs.filter { it.status== signStatusAdapter.data.get(position).status})
            }
            signAdapter.notifyDataSetChanged()

        }

        binding.rvSign.apply {
            adapter=signAdapter
            layoutManager=LinearLayoutManager(this@SignListActivity)
        }

        vm.getSigns(SPUtils.getInstance().getInt(Const.UID))

        binding.signSv.setOnQueryTextListener(object :OnQueryTextListener{
            override fun onQueryTextSubmit(query: String?): Boolean {
                return true
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                signAdapter.data.clear()
                signAdapter.addData(signs.filter {
                    if(it.morningCheckIn.isNullOrEmpty())
                        it.afternoonCheckIn.split(" ")[0].contains(newText!!)
                    else
                        it.morningCheckIn.split(" ")[0].contains(newText!!)
                })
                signAdapter.notifyDataSetChanged()
                return true
            }

        })

        binding.signMonth.setText(month)

        binding.signNext.setOnClickListener {
            selectByMonth(1)
        }

        binding.signPre.setOnClickListener {
            selectByMonth(-1)
        }
    }

    fun selectByMonth(clickNum:Int){
        calendar.add(Calendar.MONTH,clickNum)
        val newMonth = SimpleDateFormat("MM").format(calendar.time)
        binding.signMonth.setText(newMonth)

        signAdapter.data.clear()
        signs=signsByMonth.filter {
            if(it.morningCheckIn.isNullOrEmpty())
                it.afternoonCheckIn.split("-")[1]==newMonth
            else
                it.morningCheckIn.split("-")[1]==newMonth
        }
        signAdapter.addData(signs)
        signAdapter.notifyDataSetChanged()
    }
}